10.3 Examples for requesting key recovery for a person

Assume you have a person with the following ID:

957463EB-320C-42E8-BC5E-CC33A6AF960E

This person has an archived certificate that you want to recover; the certificate ID is:

1947061b-6ece-4a16-9c1a-6d64ea6721b8

Note: This is not the same as the certificate serial number. You can find the certificate ID either through the API (for example, use GET /api/Certificates with the serial number specified as the certSerialNo parameter) or by checking the ObjectID field in the Certificates table in the MyID database.

You have created a credential profile for key recovery, which has an ID of:

fb3c5847-9412-40e3-ba0c-69d16f37f75f

You can use the API to request key recovery for the person using the POST api/people/{id}/requests endpoint, specifying the Recover to New Device operation ID (op=100430).

These examples assume your server is on myserver.example.com, and that you have already obtained an access token; <YOUR-TOKEN> is used as a placeholder.

10.3.1 cURL example

Copy
curl.exe -X "POST" "https://myserver.example.com/rest.core/api/People/957463EB-320C-42E8-BC5E-CC33A6AF960E/requests?f=false&op=100430" -H "Authorization: Bearer <YOUR-TOKEN>" -H "accept: application/json" -H "x-api-version: 1" -H "Content-Type: application/json" -d "{""credProfile"": {""id"": ""fb3c5847-9412-40e3-ba0c-69d16f37f75f""},""recoverCertIds"": [""1947061b-6ece-4a16-9c1a-6d64ea6721b8""]}"

10.3.2 Python example

Copy
import requests
import json

# Set the server
server = "myserver.example.com"

# ID of the person in the MyID database
personID = "957463EB-320C-42E8-BC5E-CC33A6AF960E"

# Credential profile ID
credProfileID = "fb3c5847-9412-40e3-ba0c-69d16f37f75f"

# Certificate ID
certID = "1947061b-6ece-4a16-9c1a-6d64ea6721b8"

# Set the access token
token = "<YOUR-TOKEN>"

# Build the payload
requestData = {
  "credProfile": {
    "id": credProfileID
  },
  "recoverCertIds": [
    certID
  ]
}

request = json.dumps(requestData)

# Call the API
response = requests.post(
    "https://" + server + "/rest.core/api/People/" + personID + "/requests?f=false&op=100430",
    headers={"Authorization": "Bearer " + token,
            "Content-Type": "application/json",
            "accept": "application/json"}, 
    data=request)

# Display the response
if response.status_code==200:
    returnedData = json.loads(response.text)
    print(returnedData)
else:
    print("An error occurred:")
    returnedData = json.loads(response.text)
    print("Error code: " + returnedData["code"])
    print("Error message: " + returnedData["message"])

10.3.3 PowerShell example

Copy
# Set the server
$server = "myserver.example.com"

# ID of the person in the MyID database
$personID = "957463EB-320C-42E8-BC5E-CC33A6AF960E"

# Credential profile ID
$credProfileID = "fb3c5847-9412-40e3-ba0c-69d16f37f75f"

# Certificate ID
$certID = "1947061b-6ece-4a16-9c1a-6d64ea6721b8"

# Set the access token
$token = "<YOUR-TOKEN>"

# Build the payload
$requestData = "{'credProfile': {'id': '"+ $credProfileID + "'},'recoverCertIds': ['" + $certID + "']}"

# Set up the call for the API
$authHeader = @{
    'Content-Type'='application/json'
    'Authorization'="Bearer $token"
    'x-api-version'= '1'
 }
$URI = 'https://' + $server + '/rest.core/api/People/' + $personID + '/requests?f=false&op=100430'
$person  = @{
    Headers =  $authHeader
    Uri = $URI
    Method = "POST"
    Body = $requestData
}

# Display the response
try {
    $result = Invoke-WebRequest @person | ConvertFrom-Json
    Write-Host $result
}
catch {
    $result = $_.Exception.Response.GetResponseStream()
    $reader = New-Object System.IO.StreamReader($result)
    $reader.BaseStream.Position = 0
    $reader.DiscardBufferedData()
    $responseBody = $reader.ReadToEnd() | ConvertFrom-Json
    Write-Host "An error occurred:"
    Write-Host "Error code:" $responseBody.code
    Write-Host "Error message:" $responseBody.message
}